package com.sgb.kjwl.view.ui.activity.self.carMaster;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityHistoryQuoteBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.HistoryOfferEntity;
import com.sgb.kjwl.view.constant.RouterKey;
import com.sgb.kjwl.view.ui.activity.self.offer.TruckApplyActivity;
import com.sgb.kjwl.view.ui.adapter.self.carMasters.HistoricalQuoteAdapter;
import com.sgb.kjwl.viewmodel.self.carMakers.HistoricalQuoteModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Describe:
 * Author:  yangLei
 * Date:    2021/4/7
 */
public class HistoricalQuoteActivity extends AppActivity {

    HistoricalQuoteModel viewModel;
    ActivityHistoryQuoteBinding binding;
    private HistoricalQuoteAdapter adapter;
    private List<HistoryOfferEntity> offerEntities = new ArrayList<>();
    private String sourceNo;
    private boolean isGeneral;//

    @Override
    protected void initView() {
        sourceNo = getIntent().getStringExtra("no");
        isGeneral = getIntent().getBooleanExtra(RouterKey.BOOLEAN, false);
        if (TextUtils.isEmpty(sourceNo))
            finish();
        binding = DataBindingUtil.setContentView(this, R.layout.activity_history_quote);
        binding.topBar.titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
        binding.topBar.titleView.setText("历史报价");
        binding.topBar.ivBack.setOnClickListener(v -> finish());
        viewModel = new ViewModelProvider(this).get(HistoricalQuoteModel.class);
        if (adapter == null)
            adapter = new HistoricalQuoteAdapter(getActivity(), offerEntities, isGeneral);
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.shape_list_divider_10dp));
        binding.recyclerView.addItemDecoration(divider);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recyclerView.setAdapter(adapter);
        binding.smartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                viewModel.getHistoryData(HistoricalQuoteActivity.this, sourceNo, false);
                binding.smartRefresh.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                viewModel.getHistoryData(HistoricalQuoteActivity.this, sourceNo, true);
                binding.smartRefresh.finishRefresh();
            }
        });

        viewModel.refreshLiveData.observe(this, offerEntities -> showHistoryData(offerEntities));
        viewModel.loadMoreLiveData.observe(this, offerEntities -> loadMoreData(offerEntities));
    }

    public static void start(Context context, String goodsSourceNo, boolean isGeneral) {
        Intent intent = new Intent(context, HistoricalQuoteActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("no", goodsSourceNo);
        intent.putExtra(RouterKey.BOOLEAN, isGeneral);
        context.startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewModel.getHistoryData(HistoricalQuoteActivity.this, sourceNo, true);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);

    }

    public void showHistoryData(List<HistoryOfferEntity> entities) {
        if (entities == null)
            return;
        offerEntities.clear();
        if (entities.size() <= 0) {
            binding.recyclerView.setVisibility(View.GONE);
            binding.emptyView.setVisibility(View.VISIBLE);
        } else {
            offerEntities.addAll(entities);
            adapter.notifyDataSetChanged();
            binding.recyclerView.setVisibility(View.VISIBLE);
            binding.emptyView.setVisibility(View.GONE);
        }

    }

    public void loadMoreData(List<HistoryOfferEntity> entities) {
        if (entities == null || entities.size() <= 0)
            return;
        if (entities.size() > 0) {
            offerEntities.addAll(entities);
            adapter.notifyDataSetChanged();
            binding.recyclerView.setVisibility(View.VISIBLE);
            binding.emptyView.setVisibility(View.GONE);
        }
    }
}
